package com.wildlife.springboot.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;

@RestController
@   RequestMapping("/upload")
public class FileUploadController {

    @Value("${upload.dir:sounds/}") // 配置上传文件的存储目录
    private String uploadDir;

    @PostMapping("/sound")
    public String uploadSound(@RequestParam("file") MultipartFile file) throws IOException {
        // 获取文件名
        String fileName = StringUtils.cleanPath(file.getOriginalFilename());

        // 获取存储路径
        Path targetLocation = Paths.get(uploadDir).resolve(fileName);

        // 保存文件
        file.transferTo(targetLocation.toFile());

        // 返回音频文件的相对路径或 URL
        return "/sounds/" + fileName;
    }
}